﻿using DomainService.IService.Sys;
using DomainService.Service.Sys;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Model.BusinessModel;
using Model.BusinessModel.Input;
using Model.BusinessModel.InPut;
using Model.BusinessModel.OutPut;
using Model.BusinessModel.OutPut.sys;
using Model.DomainModel.Sys;
using OverallAuth_WebApi.PlugIn;
using OverallAuth_WebApi.PlugInU;
using System.IdentityModel.Tokens.Jwt;
using Utility;
using Utility.Enum;

namespace OverallAuth_WebApi.Controllers.Sys
{
    /// <summary>
    /// 用户模块
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    [ApiExplorerSettings(GroupName = nameof(ModeuleGroupEnum.SysUser))]
    public class SysUserController : BaseController
    {
        #region 构造实列化

        /// <summary>
        /// 用户服务
        /// </summary>
        public ISysUserService _userService;

        /// <summary>
        /// 登录日志服务
        /// </summary>
        public ISysLoginLogService _sysLoginLogService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userService"></param>
        /// <param name="sysLoginLogService"></param>
        public SysUserController(ISysUserService userService, ISysLoginLogService sysLoginLogService)
        {
            _userService = userService;
            _sysLoginLogService = sysLoginLogService;
        }

        #endregion

        #region 接口

        /// <summary>
        /// 获取登录日志,并写入数据库
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ReceiveStatus<SysLoginLog>> GetLoginOrInsert()
        {
            ReceiveStatus<SysLoginLog>? receiveStatus = new();
            var model = GetLoginUserMsg();

            //地理位置信息
            var positionModel = await LoginPlugIn.GetLoginPositionMsg(HttpContext);

            SysLoginLog sysLoginLog = new SysLoginLog
            {
                Id = Guid.NewGuid().ToString(),
                CorporationKey = model.CorporationKey ?? string.Empty,
                UserId = model.UserId ?? string.Empty,
                Ip = positionModel?.ip,
                Country = positionModel?.data?.country,
                Prov = positionModel?.data?.prov,
                City = positionModel?.data?.city,
                Area = positionModel?.data?.area,
                Isp = positionModel?.data?.isp,
                Lng = positionModel?.data?.lng,
                Lat = positionModel?.data?.lat,
                CreateTime = DateTime.Now,
                CreateUser = model.UserId ?? string.Empty
            };
            _sysLoginLogService.Insert(sysLoginLog);
            receiveStatus.data = new List<SysLoginLog>() { sysLoginLog };
            return receiveStatus;
        }

        /// <summary>
        /// 查询用户
        /// </summary>
        /// <param name="departmentId">部门id</param>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<SysUserOutPut> GetUser(string? departmentId)
        {
            return _userService.GetUser(GetLoginUserMsg().CorporationKey ?? string.Empty, departmentId);
        }

        /// <summary>
        /// 获取公司人员
        /// </summary>
        /// <param name="pageResultModel"></param>
        /// <returns></returns>
        [HttpPost]
        public ReceiveStatus<SysUserOutPut> GetCorporationUser(PageResultModel pageResultModel)
        {
            ReceiveStatus<SysUserOutPut> receiveStatus = new();
            var result = _userService.GetCorporationUser(pageResultModel);
            receiveStatus.data = result.data;
            receiveStatus.total = result.total;
            return receiveStatus;
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="sysUser">用户数据</param>
        [HttpPost]
        public ReceiveStatus Insert(SysUser sysUser)
        {
            sysUser.CreateTime = DateTime.Now;
            sysUser.CreateUser = GetLoginUserMsg().UserId;
            return _userService.Insert(sysUser);
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="sysUser">用户数据</param>
        [HttpPost]
        public ReceiveStatus Update(SysUser sysUser)
        {
            return _userService.Update(sysUser);
        }

        /// <summary>
        /// 设置用户是否启用
        /// </summary>
        /// <param name="updateIsOpenInPut">修改模型</param>
        [HttpPost]
        public ReceiveStatus SetUserIsOpen(UpdateIsOpenInPut updateIsOpenInPut)
        {
            return _userService.SetUserIsOpen(updateIsOpenInPut);
        }

        /// <summary>
        /// 获取登录日志
        /// </summary>
        /// <param name="pageResultModel">分页模型</param>
        /// <returns>返回指定数据</returns>
        [HttpPost]
        public ReceiveStatus<LoginLogOutPut> GetLoginLogMsg(PageResultModel pageResultModel)
        {
            ReceiveStatus<LoginLogOutPut> receiveStatus = new();
            var result = _sysLoginLogService.GetLoginLogMsg(pageResultModel);
            receiveStatus.data = result.data;
            receiveStatus.total = result.total;
            return receiveStatus;
        }

        /// <summary>
        /// 设置用户角色
        /// </summary>
        /// <param name="userOrRoleInput">传入模型</param>
        [HttpPost]
        public ReceiveStatus SetUserRole(UserRoleInput userOrRoleInput)
        {
            return _userService.SetUserRole(userOrRoleInput, GetLoginUserMsg().UserId ?? string.Empty);
        }

        /// <summary>
        /// 根据用户id获取对应角色
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>返回用户所属角色key</returns>
        [HttpGet]
        public ReceiveStatus<int> GetUserRole(string userId)
        {
            var loginModel = GetLoginUserMsg();
            return _userService.GetUserRole(userId, loginModel.CorporationKey);
        }

        /// <summary>
        /// 获取公司人员
        /// </summary>
        /// <param name="corporationKey">公司key</param>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<TreeOutPut> GetCorporationUser(string corporationKey)
        {
            return _userService.GetCorporationUser(corporationKey);
        }

        /// <summary>
        /// 根据角色获取绑定用户
        /// </summary>
        /// <param name="roleId">角色id</param>
        /// <returns>返回角色绑定用户id</returns>
        [HttpGet]
        public ReceiveStatus<string> GetBindUser(int roleId)
        {
            return _userService.GetBindUser(roleId, GetLoginUserMsg().CorporationKey ?? string.Empty);
        }

        /// <summary>
        /// 根据公司key,获取用户,用于登录
        /// </summary>
        /// <param name="corporationKey">公司key</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public ReceiveStatus<SelectOutPut> GetUserByCorporationKey(string corporationKey)
        {
            return _userService.GetUserByCorporationKey(corporationKey);
        }
        #endregion

    }
}
